#include <QtCore/QCoreApplication>
#include <iostream>
#include <string>
//#include <stdlib.h>
#include <QtDebug>
#include <cbridge.h>
//#include <time.h>
#include <unistd.h>


using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    char s_port[20];
    char temp_str[50];
    int u_r_port;
    int u_s_port;
    int batcapacity;

    cout << "########################################################################" << endl;
    cout << "###             Welcome to MK-QGroundControl Bridge v1.0a!           ###" << endl;
    cout << "### Developed by Ofir Semel and Or Fattal - Senior Project, BGU 2011 ###" << endl;
    cout << "########################################################################" << endl << endl;

    cout << "Please enter serial port (default: /dev/ttyUSB0): ";
    cin.getline (s_port,20);
    if (!strcmp (s_port,""))
        strcpy (s_port, "/dev/ttyUSB0");
    cout << "# s_port = " << s_port << endl;

    cout << "Please enter UDP receive port (default: 14555): ";
    cin.getline (temp_str,50);
    if (!strcmp (temp_str,""))
        u_r_port = 14555;
    else
        u_r_port = atoi(temp_str);
    cout << "# u_r_port = " << u_r_port << endl;

    cout << "Please enter UDP send port (default: 14550): ";
    cin.getline (temp_str,50);
    if (!strcmp (temp_str,""))
        u_s_port = 14550;
    else
        u_s_port = atoi(temp_str);

    cout << "Please enter current battary capacity in mAh (default: 2650): ";
    cin.getline (temp_str,50);
    if (!strcmp (temp_str,""))
        batcapacity = 2650;
    else
        batcapacity = atoi(temp_str);
    cout << "# batcapacity = " << batcapacity << endl;

    CBridge *bridge = new CBridge(u_r_port,u_s_port,s_port, batcapacity);
    bridge->StartListening();

    cout << endl;

    cout << "########################################################################" << endl;
    cout << "## System init check list:                                            ##" << endl;
    cout << "##   - Serial modem is connected properly.                            ##" << endl;
    cout << "##   - UDP ports are compatiable with QGC.                            ##" << endl;
    cout << "##   - LocalHost in QGC is set to 127.0.0.1.                          ##" << endl;
    cout << "##   - MK is on and its modem is connected properly.                  ##" << endl;
    cout << "########################################################################" << endl;


    cout  << endl << "# Type \"help\" for list of all available commands." << endl;

    while (1)
    {
        cout << "MK_QGC (" << bridge->CurrentUARTAddress << ")>>";
        cin.getline (temp_str,50);

        QByteArray input (temp_str);
        input = input.toLower();
        input = input.simplified();

        if (input == "")
        {
            continue;
        }

        else if (input == "help")
        {
            cout << "# Available Commands:" << endl;
            cout << "# \t\t loss                           # print packet loss rate.                                        " << endl;
            cout << "# \t\t pflags                         # print MK flags status.                                         " << endl;
            cout << "# \t\t reqver                         # requests version from the directed MK unit.                    " << endl;
            cout << "# \t\t reset                          # requests reset from MK.                                        " << endl;
            cout << "# \t\t redirect N                     # redirects MK UART. N values can be 1: FC, 3: MK3MAG, 4: MKGPS. " << endl;
            cout << "# \t\t magic                          # sends Magic Code to MK.                                        " << endl;
            cout << "# \t\t help                           # displays this help.                                            " << endl;
            cout << "# \t\t exit                           # exits the program.                                             " << endl;
        }

        else if (input == "exit")
        {
            cout << "# GoodBye." << endl;
            exit (0);
        }

        else if (input == "magic")
        {
            bridge->SendMagicCode();
        }

        else if (input.startsWith("redirect"))
        {
            bridge->redirect_UART(input[9] - '0');
        }

        else if (input == "pflags")
        {
            bridge->EnablePrintFlags = true;
        }


        else if (input == "reqver")
        {
                bridge->request_MK_version(ANY_ADDRESS);
        }

        else if (input == "loss")
        {
                bridge->PrintLoss();
        }

        else if (input == "reset")
        {
                bridge->request_MK_Reset();
        }

        else
        {
           cout << "# Unknown command." << endl;
        }

    }

    return a.exec();
}
